﻿<?php
	require_once("function.php");
	//echo '<span style="color:#00eeff;">请鼠标选择</span>';	
function spell1($char,$row,$i=0,$j=0,$q=0){
		$k=0;
		$x=$i;
		$f=0;
		//echo "<br />q=".$q;
		for(;;){
	  //echo "<br />i=".$i;echo "<br />j=".$j;echo "<br />k=".$k;echo "<br />char[i]=".$char[$i];echo "<br />row[j][k]=".$row[$j][$k];
		if(isset($row[$j][$k]) and isset($char[$i]) and !strcmp($char[$i],$row[$j][$k]))
		{
			if(!isset($char[$i+1]) and !isset($row[$j+1]) or !isset($char[$i+1]))
			{
				return 1;
			}
			$k++;$i++;
			//echo "<br />k=".$k;
			//echo "<br /><br /><br />";
			}
		else
		{
			//echo "<br /><br /><br />";
			
				if(!isset($row[$j+1]))
				{
					return 0;
					}
				$j++;
				if($x!=$i)
				{
				$q++;
			}
			if($q< $j)
		{return 0;}
			return spell1($char,$row,$i,$j,$q);
			}
	}
		}
	
	function checkStr($str){
    $output='';
    @$a=ereg('['.chr(0xa1).'-'.chr(0xff).']', $str);
    @$b=ereg('[0-9]', $str);
    @$c=ereg('[a-zA-Z]', $str);
    if($a && $b && $c){ $output=0;}//'汉字数字英文的混合字符串';
    elseif($a && $b && !$c){ $output=0;}//'汉字数字的混合字符串';
    elseif($a && !$b && $c){ $output=0;}//'汉字英文的混合字符串';
    elseif(!$a && $b && $c){ $output=0;}//'数字英文的混合字符串';
    elseif($a && !$b && !$c){ $output=1;}//'纯汉字';
    elseif(!$a && $b && !$c){ $output=0;}//'纯数字';
    elseif(!$a && !$b && $c){ $output=0;}//'纯英文';
    return $output;
}
	// PHP5 Implementation - uses MySQLi.
	// mysqli('localhost', 'yourUsername', 'yourPassword', 'yourDatabase');	
	conn();
	mysql_query("SET NAMES 'utf8'");
				if(isset($_POST['queryString'])) 
				{
				    $data=explode(':',$_POST['queryString']);
					$queryString = $data[0];
					$j = $data[1];
						$queryString = mysql_real_escape_string(strtoupper($queryString));
						// Is the string length greater than 0?
					if(strlen($queryString) >0) 
					{	switch($j){
						case 1:
						case 2:
						case 3:$query = mysql_query("SELECT sta_china,sta_spell,count(tra_station) FROM station,tra WHERE sta_china=tra_station and (sta_china LIKE '$queryString%' or sta_spell LIKE '$queryString[0]%') group by sta_china order by count(tra_station) desc");
					         $p=0;
					         break;
				    case 4:$query = mysql_query("SELECT type_tra_num  FROM type WHERE type_tra_num  LIKE '$queryString%'");
						       $p=1;
						       break;
						}
						$i=0;
						$cc=0;
						if($query) 
						{
								while ($result = mysql_fetch_row($query))
								{			
									
									if($p==0)
									{
									$row=explode(' ',$result[1]);	
									//print_r($row);
									if(strcmp($cc,$result[0])!=0 and spell($queryString,$row) or checkStr($queryString))
									{
										echo '<li type=none onClick="fill('.$j.',\''.$result[0].'\');">'.$result[0].'</li>';
										$i=$i+1;
										if($i>8)break;						
									}
								}
								if($p==1)
									{
										if(strcmp($cc,$result[0])!=0)
										{
											echo '<li type=none onClick="fill('.$j.',\''.$result[0].'\');">'.$result[0].'</li>';
										$i=$i+1;
										if($i>8)break;	
											}
										}
								$cc=$result[0];	
							}
						} 
						else 
						{
							echo 'ERROR: There was a problem with the query.';
						}
					} 
					else
					{} // There is a queryString.
				} 
				else 
				{
			echo 'There should be no direct access to this script!';
				}
		
?>